package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody Map<String,String> map){
        //随机生成验证码
        //Integer code = ValidateCodeUtils.generateValidateCode(4);
        String code="1234";
        //得到前端传送的手机号
        String phone = map.get("phone");
        //判断Redis中的验证码有无过期  没过期 不允许重新发送
        if (redisTemplate.hasKey(phone)){
            return R.error("上一次验证码还未过期 不允许重新发送短信");

        }
        log.info(phone);
         //SMSUtils.sendMessage("瑞吉外卖","SMS_465789",phone,code);
            //将验证码存入session
       // session.setAttribute(phone,code);
        //将验证码存入reids中 并将设置为5分钟过期
        redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);

             return R.success("短信验证码发送成功");
    }

    //登录功能
    @PostMapping("/login")
    public R<String> login(@RequestBody Map<String,String> map,HttpSession session){
        //得到前端传递回来的手机号与验证码
        String phone = map.get("phone");
        String code = map.get("code");
        log.info(code);
        /*//从session中取的短信验证码
        String code1 = (String) session.getAttribute(phone);
        log.info(code1);*/
        //从redis中取验证码然后进行比较
        String  redisCode = (String) redisTemplate.opsForValue().get(phone);
        if (StringUtils.isEmpty(redisCode) || !redisCode.equals(code)){

            return R.error("验证码错误,登录失败");
        }
            //判断是否是新用户,是的话 手机号保存至数据库
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
         lqw.eq(User::getPhone, phone);
        User user = userService.getOne(lqw);
        if (user==null){
            User user1 = new User();
            user1.setPhone(phone);
            user1.setStatus(1);
            userService.save(user1);
        }else {
            //老用户判断Status是否为启用
            Integer status = user.getStatus();
            if (status==0){
                return R.error("账号已禁用");
            }
        }
        //登录成功 从Redis中把验证码删除
        redisTemplate.delete(phone);
        //登录成功  把手机号存入session
        session.setAttribute("user",user.getId());


        return R.success("登录成功");

    }
    @PostMapping("/loginout")
    public R<String> loginout(HttpSession session){
        session.removeAttribute("user");

        return R.success("");
    }



}